package wiz.client.ui.viewimpl;

import wiz.client.WizStrings;
import wiz.client.activities.playhand.NumberValueSelector;
import wiz.client.activities.playhand.PlayHandView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.TakesValue;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasVisibility;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PlayHandViewImpl extends Composite implements PlayHandView {

	private static PlayHandViewImplUiBinder uiBinder = GWT.create(PlayHandViewImplUiBinder.class);
	private final WizStrings fWizStrings = GWT.create(WizStrings.class);


	public static final int kHeaderRow = 0;
	public static final int kScoreRow = 1;
	public static final int kBidRow = 2;
	public static final int kMadeRow = 3;
	
	
	interface PlayHandViewImplUiBinder extends UiBinder<Widget, PlayHandViewImpl> {
	}

	@UiField
	Button fStartRoundButton;

	@UiField
	Button fDoneRoundButton;

	@UiField
	SpanElement fBidValidationSpan;

	@UiField
	SpanElement fScoreValidationSpan;
	
	@UiField
	DivElement fScoreValidationDiv;
	
	@UiField
	DivElement fBidValidationDiv;
	
	
	@UiField
	NumberValueSelector fRoundSizeSelector;
	
	@UiField
	FlexTable fRoundBidsAndScoresTable;
	
	private Label fTotalBidLabel;

	

	public PlayHandViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		initTable();
	}

	private void initTable() {
		//TODO: any way to move this to UIBinder XML? 
		
		fRoundBidsAndScoresTable.insertRow(0);
		fRoundBidsAndScoresTable.insertRow(0);
		fRoundBidsAndScoresTable.insertRow(0);
		fRoundBidsAndScoresTable.insertRow(0);
		
		
		fRoundBidsAndScoresTable.setWidget(kHeaderRow, 0, new Label());
		Label scoreLabel = new Label("Score");
		scoreLabel.setHorizontalAlignment(HorizontalAlignmentConstant.startOf(Direction.LTR));
		fRoundBidsAndScoresTable.setWidget(kScoreRow, 0, scoreLabel);
		
		
		VerticalPanel vp = new VerticalPanel();
		Label bidLabel = new Label(fWizStrings.bid());
		bidLabel.setHorizontalAlignment(HorizontalAlignmentConstant.startOf(Direction.LTR));
		vp.add(bidLabel);
		
		fTotalBidLabel = new Label();
		vp.add(fTotalBidLabel);

		fRoundBidsAndScoresTable.setWidget(kBidRow, 0, vp);
		Label tricksMadeLabel = new Label(fWizStrings.tricksMade());
		tricksMadeLabel.setHorizontalAlignment(HorizontalAlignmentConstant.startOf(Direction.LTR));
		fRoundBidsAndScoresTable.setWidget(kMadeRow, 0, tricksMadeLabel);
	}
	


	@Override
	public NumberValueSelector getRoundSizeSelector() {
		return fRoundSizeSelector;
	}
	
	@UiFactory
	NumberValueSelector createNumberValueSelector() {
		return new NumberValueSelectorImpl();
	}

	
	@Override
	public HasEnabled getStartRoundButtonVisibility() {
		return fStartRoundButton;
	}
	
	@Override
	public HasClickHandlers getStartRoundButtonClickHandlers() {
		return fStartRoundButton;
	}
	
	@Override
	public HasText getBidValidationText() {
		return new SpanHasText(fBidValidationSpan);
	}
	

	@Override
	public HasEnabled getDoneRoundButtonEnabled() {
		return fDoneRoundButton;
	}
	
	@Override
	public HasClickHandlers getDoneRoundButtonClickHandlers() {
		return fDoneRoundButton;
	}
	
	@Override
	public HasText getRoundValidationText() {
		return new SpanHasText(fScoreValidationSpan);
	}
	
	@Override
	public void setBidValidationVisible(boolean visible) {
		if(!visible){
			fBidValidationDiv.addClassName("validationLabelHidden");
		} else {
			fBidValidationDiv.removeClassName("validationLabelHidden");

		}
	}
	
	@Override
	public void setScoreValidationVisible(boolean visible) {
		if(!visible){
			fScoreValidationDiv.addClassName("validationLabelHidden");
		} else {
			fScoreValidationDiv.removeClassName("validationLabelHidden");

		}		
	}
	
	@Override
	public WizPlayerHandState createAndAddPlayerHandState() {
		
		
		Label playerName = new Label();
		Label playerScore = new Label();

		NumberValueSelector bidSelector = createNumberValueSelector();
		NumberValueSelector tricksMadeSelector = createNumberValueSelector();

		
		final int lastColumnIndex = fRoundBidsAndScoresTable.getCellCount(0);
		
		fRoundBidsAndScoresTable.setWidget(kHeaderRow, lastColumnIndex, playerName);
		fRoundBidsAndScoresTable.getCellFormatter().setStyleName(kHeaderRow, lastColumnIndex, "header");
		fRoundBidsAndScoresTable.setWidget(kScoreRow, lastColumnIndex, playerScore);
		fRoundBidsAndScoresTable.setWidget(kBidRow, lastColumnIndex, bidSelector.asWidget());
		fRoundBidsAndScoresTable.setWidget(kMadeRow, lastColumnIndex, tricksMadeSelector.asWidget());
		
		
		TakesValue<Boolean> madeBid = new TakesValue<Boolean>() {

			@Override
			public void setValue(Boolean value) {
				if(value){
					fRoundBidsAndScoresTable.getCellFormatter().addStyleName(kMadeRow, lastColumnIndex, "madeBidCell");
				} else {
					fRoundBidsAndScoresTable.getCellFormatter().removeStyleName(kMadeRow, lastColumnIndex, "madeBidCell");
				}
			}

			@Override
			public Boolean getValue() {
				return null;
			}
		};
		
		
		return new WizPlayerHandState(playerName, playerScore, bidSelector, tricksMadeSelector, madeBid);
	}

	@Override
	public void removePlayerUI(int i) {
		fRoundBidsAndScoresTable.removeCell(kHeaderRow, i + 1);
		fRoundBidsAndScoresTable.removeCell(kScoreRow, i + 1);
		fRoundBidsAndScoresTable.removeCell(kBidRow, i + 1);
		fRoundBidsAndScoresTable.removeCell(kMadeRow, i + 1);
	}
	
	@Override
	public HasText getTotalBidText() {
		return fTotalBidLabel;
	}



}

